home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-26 | 3.3 KB | 170 lines | [TEXT/CWIE] |
-
- // mail <chelly@eden.com> or surf http://www.eden.com/~chelly for feedback
- // free source code - do whatever you like with it
-
- // uniform access to mac and universal pictures through common interface
-
- #include "byteorder.h"
- #include "gepicture.h"
- #include "reschain.h"
- #include "GEOffscreen.h"
-
- #include <string.h> // for memcpy()
-
- // abstract base class
- struct GEPicture
- {
- Rect m_bounds;
-
- virtual ~GEPicture() { }
- virtual void Draw( const Rect* ) = 0;
- };
-
- #if TARGET_IS_MACOS
-
- #if 0
- #pragma mark ---------------------------------
- #pragma mark | mac picture type
- #pragma mark ---------------------------------
- #endif
-
- struct GEPicture_macos : GEPicture
- {
- PicHandle m_pic;
-
- GEPicture_macos( Handle pic );
- ~GEPicture_macos();
- virtual void Draw( const Rect* bounds );
- };
-
- GEPicture_macos::GEPicture_macos( Handle pic )
- {
- m_pic = (PicHandle) pic;
- m_bounds = (**m_pic).picFrame;
- OffsetRect( &m_bounds, -m_bounds.left, -m_bounds.top );
- }
-
- GEPicture_macos::~GEPicture_macos()
- {
- ReleaseResource( (Handle) m_pic );
- }
-
- void GEPicture_macos::Draw( const Rect* r )
- {
- DrawPicture( m_pic, r );
- }
-
- #endif
-
- #if 0
- #pragma mark ---------------------------------
- #pragma mark | universal picture type
- #pragma mark ---------------------------------
- #endif
-
- // format of resource
- struct UnivPicData
- {
- int16 h_size;
- int16 v_size;
- char pixels [1];
- };
-
- struct GEPicture_univ : GEPicture
- {
- UnivPicData* m_resource;
- char* m_pixels;
-
- GEPicture_univ( void* res );
- ~GEPicture_univ();
- virtual void Draw( const Rect* bounds );
- };
-
- GEPicture_univ::GEPicture_univ( void* res )
- {
- m_resource = (UnivPicData*) res;
-
- SwapIfRequired(&m_resource->h_size);
- SwapIfRequired(&m_resource->v_size);
-
- m_bounds.top = m_bounds.left = 0;
- m_bounds.right = m_resource->h_size;
- m_bounds.bottom = m_resource->v_size;
- m_pixels = m_resource->pixels;
-
- #if TARGET_IS_WIN95
- // MUST remap black and white
- unsigned char *tmp = (unsigned char *) m_pixels;
- for (int i =m_bounds.bottom * m_bounds.right; i > 0; --i) {
- switch (*tmp) {
- case 0x00: *tmp = 255; break;
- case 0xff: *tmp = 0; break;
- }
- tmp++;
- }
- #endif
- }
-
- GEPicture_univ::~GEPicture_univ()
- {
- ReleaseResourcePtr( m_resource );
- }
-
- void GEPicture_univ::Draw( const Rect* dest_bounds )
- {
- CGrafPtr cur_port;
- GDHandle cur_dev;
- GetGWorld( &cur_port, &cur_dev );
- GEOffscreenInfo offscreen = GetGEOffscreenInfo( cur_port );
-
- char* dest = offscreen.baseAddr;
- char* src = m_pixels;
- long src_row_bytes = m_bounds.right;
- dest += dest_bounds->top * (long) offscreen.rowBytes + dest_bounds->left;
-
- for ( int v = m_bounds.bottom; v > 0; --v )
- {
- memcpy( dest, src, src_row_bytes );
- dest += offscreen.rowBytes;
- src += m_bounds.right; // no padding between lines (row bytes == width)
- }
- }
-
- #if 0
- #pragma mark ---------------------------------
- #pragma mark | interface
- #pragma mark ---------------------------------
- #endif
-
- GEPicture* GetGEPicture( int res_id )
- {
- void* res = GetResourcePtr( 'IMAG', res_id );
- if ( res )
- return new GEPicture_univ( res );
-
- #if TARGET_IS_MACOS
- // then use mac picture
- Handle h = GetResource( 'PICT', res_id );
- if ( h )
- return new GEPicture_macos( h );
- #endif
-
- return nil;
- }
-
- Rect GetGEPictureFrame( GEPictureRef pic )
- {
- return pic->m_bounds;
- }
-
- void DrawGEPicture( GEPictureRef pic, const Rect* rect )
- {
- pic->Draw( rect );
- }
-
- void DisposeGEPicture( GEPictureRef pic )
- {
- delete pic;
- }
-
-